package org.bing.zion.core;

import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class ProcessorPool {
	private Executor executor;
	private Processor[] pool;

	private int point = 0;

	public ProcessorPool(int num) throws IOException {
		executor = Executors.newFixedThreadPool(num);
		pool = new Processor[num];
		for (int i = 0; i < num; i++) {
			pool[i] = new Processor();
			startup(i, pool[i]);
		}
	}

	public void startup(final int no, final Processor p) {
		executor.execute(new Runnable() {
			public void run() {
				Thread.currentThread().setName("seda-processor-" + no);
				p.run();
			}
		});
	}

	public Processor nextProcessor() {
		// TODO impl load tuning stragy, ex count session num
		point++;
		if (point > pool.length - 1) {
			point = 0;
		}
		return pool[point];
	}
}
